/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverGUI;

import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBaseIterators;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;

final class DatePicker {
        int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
        int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
        JLabel l = new JLabel("", JLabel.CENTER);
        String day = "";
        JDialog d;
        JButton[] button = new JButton[49];
        Vector<String> array_year = new Vector<>();
        String[] array_month = {"1","2","3","4","5","6","7","8","9","10","11","12"};
        public void createArrayYear(){
            for(int i= 1900 ; i <= year ; i++){
                array_year.add(String.valueOf(i));
            }
        }
        JComboBox cb_year = new JComboBox(array_year);
        JComboBox cb_month = new JComboBox(array_month);
        public DatePicker(JFrame parent) {
                createArrayYear();
                cb_month.setSelectedIndex(month); 
                cb_month.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                       // throw new UnsupportedOperationException("Not supported yet.");
                       month = Integer.parseInt(String.valueOf(cb_month.getSelectedItem())) - 1;
                       year = Integer.parseInt(String.valueOf(cb_year.getSelectedItem()));
                       displayDate(); 
                    }
                }); 
                cb_year.setSelectedIndex(array_year.size() - 1); 
                cb_year.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet.");
                       month = Integer.parseInt(String.valueOf(cb_month.getSelectedItem())) - 1;
                       year = Integer.parseInt(String.valueOf(cb_year.getSelectedItem()));
                       displayDate();
                    }
                }); 
                d = new JDialog();
                d.setModal(true); 
                d.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
                d.setAlwaysOnTop(true);
                String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
                JPanel p1 = new JPanel(new GridLayout(7, 7));
                p1.setPreferredSize(new Dimension(430, 120));

                for (int x = 0; x < button.length; x++) {
                        final int selection = x;
                        button[x] = new JButton();
                        button[x].setFocusPainted(false);
                        button[x].setBackground(Color.white);
                        if (x > 6)
                                button[x].addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent ae) {
                                                day = button[selection].getActionCommand();
                                                
                                                d.dispose();
                                        }
                                });
                        if (x < 7) {
                                button[x].setText(header[x]);
                                button[x].setForeground(Color.red);
                        }
                        p1.add(button[x]);
                }
                JPanel p2 = new JPanel(new GridLayout(1, 3));
                JButton previous = new JButton("<< Previous");
                previous.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                                month--;
                                displayDate();
                        }
                });
                p2.add(previous);
                p2.add(l);
                p2.add(cb_month);
                p2.add(cb_year);
                JButton next = new JButton("Next >>");
                next.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                                month++;
                                displayDate();
                        }
                });
                p2.add(next);
                if(!"datepicker".equals(parent.getFocusOwner().getName())){
                    return;
                }
                d.add(p1, BorderLayout.CENTER);
                d.add(p2, BorderLayout.SOUTH);
                d.pack();
                d.setLocation(parent.getFocusOwner().getLocationOnScreen().x,parent.getFocusOwner().getLocationOnScreen().y + 27); 
                displayDate();
                d.setVisible(true);
                
        }

        public void displayDate() {
                for (int x = 7; x < button.length; x++)
                        button[x].setText("");
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                                "MMMM yyyy");
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set(year, month, 1);
                int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
                int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
                for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
                        button[x].setText("" + day);
                l.setText(sdf.format(cal.getTime()));
                d.setTitle("Date Picker");
        }

        public String setPickedDate() {
                if (day.equals(""))
                        return "";
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                                "MM-dd-yyyy");
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set(year, month, Integer.parseInt(day));
                return sdf.format(cal.getTime());
        }
}

class Picker {
        public static void main(String[] args) {
                JLabel label = new JLabel("Selected Date:");
                final JTextField text = new JTextField(20);
                JButton b = new JButton("popup");
                JPanel p = new JPanel();
                p.add(label);
                p.add(text);
                p.add(b);
                final JFrame f = new JFrame();
                f.getContentPane().add(p);
                f.pack();
                f.setVisible(true);
                b.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                               text.setText(new DatePicker(f).setPickedDate());
                        }
                });
        }
}